package online.controller;

import lombok.extern.slf4j.Slf4j;
import online.config.DelayedQueueConfig;
import online.config.TtlQueueConfig;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: 狼芒
 * @Date: 2021/11/7 17:29
 * @Descrintion: RabbitMq控制层
 * @version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/rabbitmq")
public class RabbitMqController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 接收消息,也就是消息的生产者
     */
    @GetMapping("/sendMsg/{message}")
    public String sendMsg(@PathVariable String message) {
        log.error("当前时间：{}, message: {}", System.currentTimeMillis(), message);
        rabbitTemplate.convertAndSend(TtlQueueConfig.COMMON_SWITCH, TtlQueueConfig.COMMON_SWITCH_TO_COMMON_FASTER_QUEUE_PATH, "消息来自ttl为10S的队列" + message);
        rabbitTemplate.convertAndSend(TtlQueueConfig.COMMON_SWITCH, TtlQueueConfig.COMMON_SWITCH_TO_COMMON_SLOWER_QUEUE_PATH, "消息来自ttl为40S的队列" + message);
        return String.valueOf(System.currentTimeMillis());
    }

    /**
     * 接收包含过期时间的消息
     */
    @GetMapping("/sendExpirationMsg/{message}/{ttlTime}")
    public String sendExpirationMsg(@PathVariable("message") String message, @PathVariable("ttlTime") String ttlTime) {
        log.error("当前时间：{},sendExpirationMsg ttlTime:{} message: {}", System.currentTimeMillis(), ttlTime, message);
        rabbitTemplate.convertAndSend(TtlQueueConfig.COMMON_SWITCH, TtlQueueConfig.COMMON_SWITCH_TO_COMMON_ALL_TIME_QUEUE_PATH, message,
                msg -> {
                    msg.getMessageProperties().setExpiration(ttlTime);
                    return msg;
                });
        return String.valueOf(System.currentTimeMillis());
    }
    /**
     * 接收包含过期时间的消息
     */
    @GetMapping("/sendDelayedMsg/{message}/{ttlTime}")
    public String sendDelayedMsg(@PathVariable("message") String message, @PathVariable("ttlTime") Integer ttlTime) {
        log.error("当前时间：{},sendExpirationMsg Delayed Queue Time:{} message: {}", System.currentTimeMillis(), ttlTime, message);
        CorrelationData correlationData = new CorrelationData();
        correlationData.setReturned(new ReturnedMessage(new Message(message.getBytes(), new MessageProperties()),0,"", DelayedQueueConfig.DELAYED_EXCHANGE, DelayedQueueConfig.DELAYED_ROUTING_KEY));
        rabbitTemplate.convertAndSend(DelayedQueueConfig.DELAYED_EXCHANGE, DelayedQueueConfig.DELAYED_ROUTING_KEY, message,
                msg -> {
                    msg.getMessageProperties().setDelay(ttlTime);
                    return msg;
                }, correlationData);
        return String.valueOf(System.currentTimeMillis());
    }

    /**
     * 报警队列收到报警消息
     */
    @GetMapping("warningMsg/{message}")
    public String warningMsg(@PathVariable("message") String message) {
        log.error("当前时间：{},sendExpirationMsg Delayed Queue message: {}", System.currentTimeMillis(), message);
        CorrelationData correlationData = new CorrelationData();
        correlationData.setReturned(new ReturnedMessage(new Message(message.getBytes(), new MessageProperties()),0,"", DelayedQueueConfig.DELAYED_EXCHANGE, DelayedQueueConfig.DELAYED_ROUTING_KEY));
        rabbitTemplate.convertAndSend(TtlQueueConfig.COMMON_SWITCH, TtlQueueConfig.COMMON_SWITCH_TO_COMMON_ALL_TIME_QUEUE_PATH, message, correlationData);

        rabbitTemplate.convertAndSend(TtlQueueConfig.COMMON_SWITCH, TtlQueueConfig.COMMON_SWITCH_TO_COMMON_ALL_TIME_QUEUE_PATH+"_error", message+"_error", correlationData);
        return String.valueOf(System.currentTimeMillis());
    }
}
